package gui;

import core.utils.Utils;
import gui.utils.ProgBar;
import gui.utils.Para;
import gui.utils.ComboBox;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class North extends JPanel implements ActionListener{

    private CardLayout pLayout, dLayout, layout_centerPanelSwap;
    private JButton homeTab,table,tree,read,history;

    private JPanel dPanel, pPanel, p2;
    private Msg messages;
    private ProgBar progress;
    private Segl1 segL1;
    private ComboBox dCombo, pCombo;

    public North(Segl1 segL1){
        super(new BorderLayout());
        this.segL1 = segL1;
        this.setBorder(BorderFactory.createEmptyBorder(0,0,-2,4));

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1,2));
        p1.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        pLayout = new CardLayout();
        pPanel = new JPanel(pLayout);
        pCombo = new ComboBox("",ComboBox.PROJECT_SELECT_AND_LINKED_DATASET_MAIN_BROWSER, this, 200, 25);
        pCombo.setToolTipText("Select Project");
        pPanel.add(pCombo, "pPanel");
        pLayout.show(pPanel, "pPanel");
        p1.add(pPanel);
 
        dLayout = new CardLayout();
        dPanel = new JPanel(dLayout);
        dCombo = new ComboBox("", ComboBox.BROWSER_DATASET_SELECT, this, 200, 25);
        dCombo.setToolTipText("Select Dataset");
        dPanel.add(dCombo, "dPanel");
        dLayout.show(dPanel, "dPanel");
        p1.add(dPanel);
        this.add(p1, BorderLayout.EAST);

        JPanel progressAndMessages = new JPanel();
        progressAndMessages.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        progressAndMessages.setLayout(new BorderLayout());

        messages = new Msg();
        progressAndMessages.add(messages, BorderLayout.CENTER);

        p2 = new JPanel();
        p2.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        layout_centerPanelSwap = new CardLayout();
        p2.setLayout(layout_centerPanelSwap);
        progress = new ProgBar();
        p2.add(progress, "prog");
        layout_centerPanelSwap.show(p2, "prog");

        progressAndMessages.add(p2, BorderLayout.EAST);

        this.add(progressAndMessages, BorderLayout.CENTER);

        JPanel p = new JPanel();
        p.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
        p.setLayout(new GridLayout(1,4));
        p.add(Utils.getButton(history = homeTab = new JButton("HOME"), 120, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Home"));
        p.add(Utils.getButton(table = new JButton("TABLE"), 120, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Table"));
        p.add(Utils.getButton(read = new JButton("READ"), 120, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Read"));
        p.add(Utils.getButton(tree = new JButton("TREE"), 120, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Tree"));
        this.add(p, BorderLayout.WEST);

        ButtonGroup group = new ButtonGroup();
        group.add(homeTab);
        group.add(table);
        group.add(tree);
        group.add(read);
    }


    
    public void resetCenterGUI(){segL1.resetCenterGUI();}

    public static int HOME_TAB = 1;
    public static int TABLE_TAB = 2;
    public static int TREE_TAB = 3;
    public static int READ_TAB = 4;

    int counter = 0;
    //after adding a project or dataset so the new stuff is displayed in the box
    public void refreshProjBrowserDropDowns(){
        pCombo = new ComboBox("", ComboBox.PROJECT_SELECT_AND_LINKED_DATASET_MAIN_BROWSER, this, 200, 25);
        counter++;
        String title = counter+"";
        pPanel.add(pCombo, title);
        pLayout.show(pPanel, title);
        resetAssociatedDatasetCombobox();
    }

    public void resetAssociatedDatasetCombobox(){
        dCombo = new ComboBox("", ComboBox.BROWSER_DATASET_SELECT, this, 200, 25);
        counter++;
        String title = counter+"";
        dPanel.add(dCombo, title);
        dLayout.show(dPanel, title);
    }    
    
    public void updatePlots(){segL1.segL2.updatePlots();}
    public void msg(String msg){messages.msg(msg);}
    public void idPrg(int which){progress.idPrg(which);}
    public void dPrg(int which, int val){progress.dPrg(which, val);}

    public void setNorthTabActiveState(int which){
        resetButtonColors();
        if(which==HOME_TAB){
            segL1.loadCard(Segl2.HOME, false);
            homeTab.setForeground(Color.RED);
            history = homeTab;
        }
        else if(which==TABLE_TAB){
            if(Para.PB!=null){
                segL1.loadCard(Segl2.TABLE, false);
                table.setForeground(Color.RED);
                history = table;
            }
            else{
                msg("Select a dataset to view");
                setNorthTabActiveState(HOME_TAB);
                history = homeTab;
            }
        }
        else if(which==TREE_TAB){
            if(Para.PB!=null){
                segL1.loadCard(Segl2.TREE, false);
                tree.setForeground(Color.RED);
                history = tree;
            }
            else{
                msg("Select a dataset to view");
                setNorthTabActiveState(HOME_TAB);
                history = homeTab;
            }
        }
        else if(which==READ_TAB){
            if(Para.PB!=null){
                segL1.loadCard(Segl2.READ, false);
                read.setForeground(Color.RED);
                history = read;
            }
            else{
                msg("Select a dataset to view");
                setNorthTabActiveState(HOME_TAB);
                history = homeTab;
            }
        }
    }
    
    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(source == homeTab){setNorthTabActiveState(HOME_TAB);}
        else if(source == table){setNorthTabActiveState(TABLE_TAB);}
        else if(source == tree){setNorthTabActiveState(TREE_TAB);}
        else if(source == read){setNorthTabActiveState(READ_TAB);}
    }

    private void resetButtonColors(){
        homeTab.setForeground(new Color(102, 102, 102));
        table.setForeground(new Color(102, 102, 102));
        tree.setForeground(new Color(102, 102, 102));
        read.setForeground(new Color(102, 102, 102));
    }
}